package com.tangyee.home_blood_center.db;

import android.database.Cursor;
import android.database.CursorWrapper;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.RecordDbSchema.RecordTable;

import java.util.Date;

/**
 * cursor的装饰类，增强功能
 */
public class RecordCursorWrapper extends CursorWrapper {
    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public RecordCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    /**
     * 获得一条记录
     * @return
     */
    public BSRecord getBsRecord() {
        long date = getLong(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.DATE));
        double dawn = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.DAWN));
        double beforeBreakfast = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.BEFORE_BREAKFAST));
        double afterBreakfast = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.AFTER_BREAKFAST));
        double beforeLunch = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.BEFORE_LUNCH));
        double afterLunch = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.AFTER_LUNCH));
        double beforeDinner = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.BEFORE_DINNER));
        double afterDinner = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.AFTER_DINNER));
        double beforeSleep = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.BEFORE_SLEEP));
        int systolicBP = getInt(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.SYSTOLIC_BP));
        int diastolicBP = getInt(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.DIASTOLIC_BP));
        double saccharificationValue = getDouble(getColumnIndex(RecordDbSchema.RecordTable.Cloumns.SACCHARIFICATION_VALUE));

        BSRecord bsRecord = new BSRecord();
        bsRecord.setDate(new Date(date));
        bsRecord.setDataBySign(1, dawn);
        bsRecord.setDataBySign(2, beforeBreakfast);
        bsRecord.setDataBySign(3, afterBreakfast);
        bsRecord.setDataBySign(4, beforeLunch);
        bsRecord.setDataBySign(5, afterLunch);
        bsRecord.setDataBySign(6, beforeDinner);
        bsRecord.setDataBySign(7, afterDinner);
        bsRecord.setDataBySign(8, beforeSleep);
        bsRecord.setDataBySign(11, systolicBP);
        bsRecord.setDataBySign(12, diastolicBP);
        bsRecord.setSaccharificationValue(saccharificationValue);

        return bsRecord;
    }

    /**
     * 获得记录总数
     * @return
     */
    public int getRecordsCount() {
        return getCount();
    }
}
